package com.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bean.BookBean;
import com.myapp.MyApp;
import com.util.GlideUtils;
import com.xiaochun.hxhj.AnswerQueActivity;
import com.xiaochun.hxhj.LoginActivity;
import com.xiaochun.hxhj.R;

import java.util.List;

/**
 * Created by fei on 2017/12/30.
 */

public class BookRecyclerViewAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public static final int TYPE_NORMAL = 5;
    public static final int TYPE_BOTTOM = 6;//底部

    private List<BookBean.DataBean> mDatas;
    private Context context;
    private MyApp myApp;

    public BookRecyclerViewAdapter2(List<BookBean.DataBean> mDatas, Context context, MyApp myApp) {
        this.mDatas = mDatas;
        this.context = context;
        this.myApp = myApp;
    }

    private View bottomView;

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }


    public void setBottomView(View bottomView) {
        this.bottomView = bottomView;
    }

    public void setDatas(List<BookBean.DataBean> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
       /* if (datas.size() > 10){
            notifyItemRangeChanged(datas.size()-10,10);
        }else{
            notifyItemRangeChanged(0,datas.size());
        }*/
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mDatas.size()) return TYPE_BOTTOM;
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (bottomView != null && viewType == TYPE_BOTTOM) {
            return new Holder(bottomView);

        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recoommend, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {

        if (getItemViewType(position) == TYPE_BOTTOM) {
            return;
        }
        final int pos = getRealPosition(viewHolder);
            final BookBean.DataBean bean = mDatas.get(pos);
            if (viewHolder instanceof Holder) {

            GlideUtils.loadRoundCircleImage(context, bean.getLogo_image(), ((Holder) viewHolder).img);
            ((Holder) viewHolder).tv_name.setText(bean.getName());
            ((Holder) viewHolder).tv_content.setText(bean.getDescription());
                if (mListener != null) {
                    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListener.onItemClick(pos);
                        }
                    });
                }


                String testId = bean.getTest_id();
                String course_id = bean.getId()+"";
                if ("0".equals(testId)){
                    ((Holder) viewHolder).tv_freetry.setVisibility(View.INVISIBLE);
                }else{
                    ((Holder) viewHolder).tv_freetry.setVisibility(View.VISIBLE);
                }
                ((Holder) viewHolder).tv_freetry.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo", Context.MODE_MULTI_PROCESS);
                        String token = sharedPreferences.getString("token", "");
                        if ("".equals(token)) {
                            Intent intent = new Intent(context, LoginActivity.class);
                            context.startActivity(intent);
                        } else {
                            Intent intent = new Intent(context, AnswerQueActivity.class);
                            intent.putExtra("catelog_id", testId + "");
                            intent.putExtra("course_id", course_id + "");
                            context.startActivity(intent);
                        }
                    }
                });

            }

    }





    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return position;
    }

    @Override
    public int getItemCount() {
        return mDatas.size() + 1;
    }

    class Holder extends RecyclerView.ViewHolder {

        private ImageView img;//图片
        private TextView tv_name;//名字
        private TextView tv_content;//介绍
        private TextView tv_seedetail;//查看详情
        private TextView tv_freetry;//查看详情

        public Holder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img_img);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_seedetail = (TextView) itemView.findViewById(R.id.tv_seedetail);
            tv_freetry = (TextView) itemView.findViewById(R.id.tv_freetry);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }



}